package org.usmile.algorithms.huawei.acm;

import java.util.Scanner;

// https://blog.csdn.net/qq_34465338/article/details/128357334
public class _Excel单元格数值统计 {
    private static String[][] excel;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int rows = sc.nextInt();
        int cols = sc.nextInt();
        sc.nextLine();

        excel = new String[rows][cols];
        for (int row = 0; row < rows; row++) {
            String[] colStrings = sc.nextLine().split(" ");
            excel[row] = colStrings;
        }

        String[] zones = sc.nextLine().split(":");
        int[] start = calculateRowCol(zones[0]);
        int[] end = calculateRowCol(zones[1]);
        int sum = 0;
        for (int row = start[0]; row <= end[0]; row++) {
            for (int col = start[1]; col <= end[1]; col++) {
                String str = excel[row][col];
                if (str.startsWith("=")) {
                    sum += calculateValue(str);
                } else {
                    sum += Integer.parseInt(str);
                }
            }
        }

        System.out.println(sum);
    }

    private static int[] calculateRowCol(String str) {
        int col = str.charAt(0) - 'A';
        int row = Integer.parseInt(str.substring(1)) - 1;

        return new int[]{row, col};
    }

    private static int calculateValue(String exp) {
        exp = exp.substring(1);
        if (exp.contains("+")) {
            int num1;
            int num2;
            String[] nums = exp.split("\\+");
            if (Character.isDigit(nums[0].charAt(0))) {
                num1 = Integer.parseInt(nums[0]);
            } else {
                int[] nums1RowCol = calculateRowCol(nums[0]);
                String num1Str = excel[nums1RowCol[0]][nums1RowCol[1]];
                if (num1Str.contains("=")) {
                    num1 = calculateValue(num1Str);
                } else {
                    num1 = Integer.parseInt(num1Str);
                }
            }
            if (Character.isDigit(nums[1].charAt(0))) {
                num2 = Integer.parseInt(nums[1]);
            } else {
                int[] nums2RowCol = calculateRowCol(nums[1]);
                String num2Str = excel[nums2RowCol[0]][nums2RowCol[1]];
                if (num2Str.contains("=")) {
                    num2 = calculateValue(num2Str);
                } else {
                    num2 = Integer.parseInt(num2Str);
                }
            }

            return num1 + num2;
        } else if (exp.contains("-")) {
            int num1;
            int num2;
            String[] nums = exp.split("-");
            if (Character.isDigit(nums[0].charAt(0))) {
                num1 = Integer.parseInt(nums[0]);
            } else {
                int[] nums1RowCol = calculateRowCol(nums[0]);
                String num1Str = excel[nums1RowCol[0]][nums1RowCol[1]];
                if (num1Str.contains("=")) {
                    num1 = calculateValue(num1Str);
                } else {
                    num1 = Integer.parseInt(num1Str);
                }
            }
            if (Character.isDigit(nums[1].charAt(0))) {
                num2 = Integer.parseInt(nums[1]);
            } else {
                int[] nums2RowCol = calculateRowCol(nums[0]);
                String num2Str = excel[nums2RowCol[0]][nums2RowCol[1]];
                if (num2Str.contains("=")) {
                    num2 = calculateValue(num2Str);
                } else {
                    num2 = Integer.parseInt(num2Str);
                }
            }

            return num1 - num2;
        } else {
            int[] numRowCol = calculateRowCol(exp);
            String numStr = excel[numRowCol[0]][numRowCol[1]];
            if (numStr.contains("=")) {
                return calculateValue(numStr);
            } else {
                return Integer.parseInt(numStr);
            }
        }
    }
}
